//
//  AddStaffViewController.m
//  Spa
//
//  Created by PangPond on 14/1/2556.
//  Copyright (c) พ.ศ. 2556 innova. All rights reserved.
//

#import "AddStaffViewController.h"
#import "Employee.h"
@interface AddStaffViewController ()

@end

@implementation AddStaffViewController

@synthesize databaseUtility =_databaseUtility;
@synthesize keyIf;
@synthesize keyStaffFirstname = _keyStaffFirstname;
@synthesize tableView = _tableView;

//cell
@synthesize addStaffFirstnameCell = _addStaffFirstnameCell;
@synthesize fieldFirstnameCell;

@synthesize addStaffLastNameCell = _addStaffLastNameCell;
@synthesize fieldLastnameCell;

@synthesize addStaffNickNameCell = _addStaffNickNameCell;
@synthesize fieldNickNameCell;

@synthesize addStaffGenderCell = _addStaffGenderCell;

@synthesize addStaffBirtdayCell = _addStaffBirtdayCell;
@synthesize fieldBirtdayCell;
@synthesize btnBirtday;

@synthesize addStaffTelCell = _addStaffTelCell;
@synthesize fieldTelCell;

@synthesize addStaffStatusCell = _addStaffStatusCell;

@synthesize addStaffAddressCell = _addStaffAddressCell;
@synthesize fieldAddressCell;

@synthesize addStaffAbilityCell = _addStaffAbilityCell;
@synthesize fieldAbilityCell;

- (IBAction)dismissKeyboard:(id)sender
{
	[sender resignFirstResponder];
    
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.databaseUtility = [[DatabaseUtility alloc]initWithDatabaseName:@"Database"];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - UITableViewDataSource

-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    //[self.tableView reloadData];
    [self loadView];
    [self viewDidLoad];
    
}
/*
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
        return [AddStaffFirstnameCell getCellHeight];
}
*/
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 8;
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSArray *employee = [self.databaseUtility fetchedResultsWithClass:@"Employee" withSortDescriptorString:@"firstname" withPredicateString:[NSString stringWithFormat:@"firstname == '%@'",self.keyStaffFirstname]];
    
    Employee *employees = [employee lastObject];
    
    
    
    if (indexPath.row == 0) {
        static NSString *CellIdentifier = @"AddStaffFirstnameCell";
        
        AddStaffFirstnameCell *cell = (AddStaffFirstnameCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            
            [[NSBundle mainBundle] loadNibNamed:@"AddStaffFirstnameCell" owner:self options:nil];
            
            cell = self.addStaffFirstnameCell;
            self.addStaffFirstnameCell = nil;
        }
        
        //config Cell
        cell.textFieldCell.text = employees.firstname;
        return cell;
        
    }if (indexPath.row == 1){
        static NSString *CellIdentifier = @"AddStaffLastNameCell";
        
        AddStaffLastNameCell *cell = (AddStaffLastNameCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            
            [[NSBundle mainBundle] loadNibNamed:@"AddStaffLastNameCell" owner:self options:nil];
            
            cell = self.addStaffLastNameCell;
            self.addStaffLastNameCell = nil;
        }
        
        //config Cell
        cell.textFieldCell.text = employees.lastname;
        return cell;
    }else if (indexPath.row == 2){
        static NSString *CellIdentifier = @"AddStaffNickNameCell";
        
        AddStaffNickNameCell *cell = (AddStaffNickNameCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            
            [[NSBundle mainBundle] loadNibNamed:@"AddStaffNickNameCell" owner:self options:nil];
            
            cell = self.addStaffNickNameCell;
            self.addStaffNickNameCell = nil;
        }
        
        //config Cell
        cell.textFieldCell.text = employees.nickname;
        return cell;
    }else if (indexPath.row == 3){
        static NSString *CellIdentifier = @"AddStaffGenderCell";
        
        AddStaffGenderCell *cell = (AddStaffGenderCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            
            [[NSBundle mainBundle] loadNibNamed:@"AddStaffGenderCell" owner:self options:nil];
            
            cell = self.addStaffGenderCell;
            self.addStaffGenderCell = nil;
        }
        
        //config Cell
        //cell.textFieldCell.text = employees;
        return cell;
    }else if (indexPath.row == 4){
        static NSString *CellIdentifier = @"AddStaffBirtdayCell";
        
        AddStaffBirtdayCell *cell = (AddStaffBirtdayCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            
            [[NSBundle mainBundle] loadNibNamed:@"AddStaffBirtdayCell" owner:self options:nil];
            
            cell = self.addStaffBirtdayCell;
            self.addStaffBirtdayCell = nil;
        }
        
        //config Cell
        cell.textFieldCell.text = employees.birtday;
        return cell;
    }else if (indexPath.row == 5){
        static NSString *CellIdentifier = @"AddStaffTelCell";
        
        AddStaffTelCell *cell = (AddStaffTelCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            
            [[NSBundle mainBundle] loadNibNamed:@"AddStaffTelCell" owner:self options:nil];
            
            cell = self.addStaffTelCell;
            self.addStaffTelCell = nil;
        }
        
        //config Cell
        cell.textFieldCell.text = employees.telephone;
        return cell;
    }else if (indexPath.row == 6){
        static NSString *CellIdentifier = @"AddStaffAddressCell";
        
        AddStaffAddressCell *cell = (AddStaffAddressCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            
            [[NSBundle mainBundle] loadNibNamed:@"AddStaffAddressCell" owner:self options:nil];
            
            cell = self.addStaffAddressCell;
            self.addStaffAddressCell = nil;
        }
        
        //config Cell
        cell.textFieldCell.text = employees.address;
        return cell;
    }else{
        static NSString *CellIdentifier = @"AddStaffAbilityCell";
        
        AddStaffAbilityCell *cell = (AddStaffAbilityCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            
            [[NSBundle mainBundle] loadNibNamed:@"AddStaffAbilityCell" owner:self options:nil];
            
            cell = self.addStaffAbilityCell;
            self.addStaffAbilityCell = nil;
        }
        
        //config Cell
        cell.textFieldCell.text = employees.ability;
        return cell;
    }
    
}

-(IBAction)BackButton:(id)sender{
    [self dismissViewControllerAnimated:YES completion:nil];
}

//validation

-(BOOL)validate: (NSString *) candidate{
    NSString *customerFirstname = @"[A-Z0-9a-zก-ฮ๑-๙.]{1,50}";
    
    //@"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
    NSPredicate *customerFirstnames = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", customerFirstname];
    
    
    return [customerFirstnames evaluateWithObject:candidate];
}


-(IBAction)SaveButton:(id)sender{
    
    if([self validate:[self.fieldFirstnameCell text]] ==1)
	{
        
        if([self validate:[fieldLastnameCell text]] ==1){
            
            
            if (keyIf == @"1") {
                
                NSArray *employees = [self.databaseUtility fetchedResultsWithClass:@"Employee" withSortDescriptorString:@"firstname" withPredicateString:[NSString stringWithFormat:@"firstname == '%@'",self.keyStaffFirstname]];
                
                Employee *employee = [employees lastObject];
                
                employee.firstname =fieldFirstnameCell.text;
                employee.lastname = fieldLastnameCell.text;
                employee.nickname =fieldNickNameCell.text;
                employee.birtday = fieldBirtdayCell.text;
                employee.telephone =fieldTelCell.text;
                employee.address = fieldAddressCell.text;
                employee.ability =fieldAbilityCell.text;
                
                [self.databaseUtility saveContext];
                [self dismissViewControllerAnimated:YES completion:nil];
                
            }else{
                
                Employee *employee = [NSEntityDescription insertNewObjectForEntityForName:@"Employee" inManagedObjectContext:self.databaseUtility.managedObjectContext];
                
                employee.firstname =fieldFirstnameCell.text;
                employee.lastname = fieldLastnameCell.text;
                employee.nickname =fieldNickNameCell.text;
                employee.birtday = fieldBirtdayCell.text;
                employee.telephone =fieldTelCell.text;
                employee.address = fieldAddressCell.text;
                employee.ability =fieldAbilityCell.text;
                

                [self.databaseUtility saveContext];
                [self dismissViewControllerAnimated:YES completion:nil];
            }
        }else{
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Message" message:@"NO1" delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
            [alert show];
        }
        
        
	}else{
		UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Message" message:@"NO" delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
		[alert show];
	}
}
//Animate
-(IBAction)btnb:(id)sender{
    NSLog(@"123");
    
    self.tableView.frame = CGRectMake(self.tableView.frame.origin.x, (self.tableView.frame.origin.y - 20 ), self.tableView.frame.size.width, self.tableView.frame.size.height);
    [UITableView commitAnimations];
    
}
/*
-(void)textFieldDidBeginEditing:(UITextField *)textField{
    NSLog(@"textFieldDidBeginEditing");
    if (textField == fieldBirtdayCell) {
        [UITableView beginAnimations:nil context:NULL];
        [UITableView setAnimationDelegate:self];
        [UITableView setAnimationDuration:0.5];
        [UITableView setAnimationBeginsFromCurrentState:YES];
        self.tableView.frame = CGRectMake(self.tableView.frame.origin.x, (self.tableView.frame.origin.y - 20 ), self.tableView.frame.size.width, self.tableView.frame.size.height);
        [UITableView commitAnimations];
    }
}

-(void)textFieldDidEndEditing:(UITextField *)textField{
    NSLog(@"textFieldDidEndEditing");
    if (textField == fieldBirtdayCell) {
        [UITableView beginAnimations:nil context:NULL];
        [UITableView setAnimationDelegate:self];
        [UITableView setAnimationDuration:0.5];
        [UITableView setAnimationBeginsFromCurrentState:YES];
        self.tableView.frame = CGRectMake(self.tableView.frame.origin.x, (self.tableView.frame.origin.y + 20 ), self.tableView.frame.size.width, self.tableView.frame.size.height);
        [UITableView commitAnimations];
    }
    
}
*/
@end
